<head>
<title>Tambah Item</title>
<link rel="stylesheet" href="<?php echo base_url()?>assets/jqueryui/themes/jquery.ui.all.css">
<link href="<?php echo base_url(); ?>assets/css/style-trx.css" rel="stylesheet" type="text/css"/>
</head>
<body style="font-size:70%;background:#CCC">
<div class="title-tambah-item">
Tambah Item Sales Order
</div>
<div class="kanvas-tambah-item">
<?php
$attributes = array('name' => 'f_so','id' => 'f_submit');
echo form_open('transaksi/sales_order_out/tambah_item',$attributes);
?>
<table width="98%" cellspacing="2" border="0" style="font-size:12px;">
<tr>
	<td colspan="2"><b>Kode</b></td>
	<td width="35%">
    	<input type="text" class="input center" name="prokode" id="prokode" placeholder="kode"/>
    </td>
	<td width="18%">
    	<input type="text" class="input" name="nama" id="nama" placeholder="Nama Barang" readonly=""/>
    </td>
    <td width="21%">
    	<input type="text" class="input" readonly="" id="satuan" name="satuan" 
        placeholder="Satuan.."/>
    </td>
</tr>
<tr>
	<td colspan="2"><b>Pilih Harga</b></td>
	<td>
    	<select name="phg" id="phg">
        	<option value="1">Harga 1
        	<option value="2">Harga 2
        	<option value="3">Harga 3
        	<option value="4">Harga 4
        	<option value="5">Harga 5
        </select>
    </td>
	<td colspan="4" align="left">&nbsp;</td>
</tr>
<tr>
	<td width="13%"><b>Harga</b></td>
	<td width="7%"><b>Rp</b></td>
    <td>
    	<input type="text" class="input right" name="harga" id="harga" 
        placeholder="Harga" onKeyUp="format_harga()"/>
    </td>
</tr>
<tr>
	<td colspan="2"><b>Qty</b></td>
	<td>
    	<input type="text" class="input center" name="qty" id="qty" placeholder="Jumlah" 
        onKeyUp="format_qty()" autocomplete="off"/>
    </td>
	<td colspan="4" align="left">&nbsp;</td>
</tr>
<tr>
	<td><b>Disc</b></td>
	<td width="7%"><b>%</b></td>
	<td>
    	<input type="text" size="10" class="inputs center" name="disc" id="disc" placeholder="Diskon.." 
        onKeyUp="format_disc()" autocomplete="off"/>
    </td>
	<td colspan="4" align="left">&nbsp;</td>
</tr>
<tr>
	<td width="13%" align="right"><div align="left"><b>Total</b>&nbsp;</div></td>
	<td width="7%"><b>Rp</b></td>
    <td width="35%">
    	<input type="text" class="input right" name="total" id="total" 
        placeholder="Total" readonly=""/>
    </td>
</tr>
<tr>
	<td align="right" colspan="6">
    	<table>
        	<tr>
            	<td>
                	<input type="submit" name="simpan" value="Simpan" class="tombol" />
                </td>
            	<td>
               	  <input type="submit" name="batal" value="Batal" class="tombol" />
                </td>
            </tr>
        </table>
    </td>
</tr>
</table>
<?php echo form_close(); ?>
</body>
</div>
	<script src="<?php echo base_url()?>assets/jqueryui/jquery-1.9.1.js"></script>
    <script src="<?php echo base_url()?>assets/jqueryui/ui/jquery.ui.core.js"></script>
    <script src="<?php echo base_url()?>assets/jqueryui/ui/jquery.ui.widget.js"></script>
    <script src="<?php echo base_url()?>assets/jqueryui/ui/jquery.ui.position.js"></script>
    <script src="<?php echo base_url()?>assets/jqueryui/ui/jquery.ui.menu.js"></script>
    <script src="<?php echo base_url()?>assets/jqueryui/ui/jquery.ui.autocomplete.js"></script>
    <script src="<?php echo base_url()?>assets/jqueryui/ui/jquery.ui.datepicker.js"></script>
   	<script src="<?php echo base_url()?>assets/jqueryui/ui/jquery.ui.tooltip.js"></script>
	<script>
        var site = "<?php echo site_url();?>";
        $(function() {
            $("#prokode").autocomplete({
            source: site+"/transaksi/search/barang",	
            minLength: 1,
            select: function(event, ui) {
                var hasil = ui.item.value.split('-');
                $('#prokode').val(hasil[0]);
                $('#nama').val(hasil[1]);
                $('#qty').val('1');
                $('#harga').val(hasil[2]);
                $('#satuan').val(hasil[3]);
                $('#total').val(hasil[2]*1);
                return false;
            }
            });
        });

		$(function () {
		  $("#phg").change(function () {
			$.get('<?php echo site_url()?>/transaksi/sales_order/pilih_harga',{ 
					phg:$(this).val(),
					prokode: document.f_so.prokode.value
				} ,function(data) {
					document.f_so.harga.value = data;
					var qty   = document.f_so.qty.value;
					var harga = document.f_so.harga.value;
					var disc = document.f_so.disc.value;            
					
					var total = harga * qty * ( 100 - disc)/100;
					
					document.f_so.total.value = total;
			});
		  });
		});

function formatNumber (obj, decimal) {
	anynum=eval(obj.value);
	divider =10;
	switch(decimal){
		case 0:
			divider =1;
			break;
		case 1:
			divider =10;
			break;
		case 2:
			divider =100;
			break;
		default:  	 //for 3 decimal places
			divider =1000;
	}
	workNum=Math.abs((Math.round(anynum*divider)/divider));
	workStr=""+workNum
	if (workStr.indexOf(".")==-1){workStr+="."}
	dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
	pStr=workStr.substr(workStr.indexOf("."))
	while (pStr.length-1< decimal){pStr+="0"}
	if(pStr =='.') pStr ='';
	//--- Adds a comma in the thousands place.    
	if (dNum>=1000) {
		dLen=dStr.length
		dStr=parseInt(""+(dNum/1000))+"."+dStr.substring(dLen-3,dLen)
	}
	//-- Adds a comma in the millions place.
	if (dNum>=1000000) {
		dLen=dStr.length
		dStr=parseInt(""+(dNum/1000000))+"."+dStr.substring(dLen-7,dLen)
	}
	retval = dStr + pStr
	//-- Put numbers in parentheses if negative.
	if (anynum<0) {retval="("+retval+")";}
	//retval =  "$"+retval
	obj.value = retval;
}

function format_harga() {
  var harga = document.f_so.harga.value;
  harga = harga.replace(/[^0-9.]/g,"");
  harga = harga.replace(".","");
  harga = harga.replace(".","");
  if(harga.length == 0) harga = "0";	

  var qty = document.f_so.qty.value;
  qty = qty.replace(/[^0-9.]/g,"");
  qty = qty.replace(".","");
  qty = qty.replace(".","");
  if(qty.length == 0) qty = "0";	
  
  var total = harga * qty;

  if(qty.length > 0) {
    if(!isNaN(qty)) {
      document.f_so.harga.value = harga;
      document.f_so.qty.value = qty;
      document.f_so.total.value = total;
      formatNumber(document.f_so.harga,0);
      formatNumber(document.f_so.qty,0);
      formatNumber(document.f_so.total,0);
     }	
  } else { document.f_so.qty.value = "0"; }			

}

function format_qty() {
  var harga = document.f_so.harga.value;
  if(harga.length == 0) harga = "0";	

  var qty = document.f_so.qty.value;
  qty = qty.replace(/[^0-9.]/g,"");
  qty = qty.replace(".","");
  qty = qty.replace(".","");
  if(qty.length == 0) qty = "0";	
  
  var total = parseFloat(harga) * qty;

  if(qty.length > 0) {
    if(!isNaN(qty)) {
      document.f_so.qty.value = qty;
      document.f_so.total.value = total;
      formatNumber(document.f_so.qty,0);
      formatNumber(document.f_so.total,1);
     }	
  } else { document.f_so.qty.value = "0"; }			

}

function format_disc() {
  var harga = document.f_so.harga.value;
  harga = harga.replace(/[^0-9.]/g,"");
  harga = harga.replace(".","");
  harga = harga.replace(".","");
  if(harga.length == 0) harga = "0";	

  var qty = document.f_so.qty.value;
  qty = qty.replace(/[^0-9.]/g,"");
  qty = qty.replace(".","");
  qty = qty.replace(".","");
  if(qty.length == 0) qty = "0";	

  var disc = document.f_so.disc.value;
  if(disc.length == 0) disc = "0";
  
  var total = harga * qty * ( 100 - disc)/100;

  if(qty.length > 0) {
    if(!isNaN(qty)) {
      document.f_so.qty.value   = qty;
      document.f_so.total.value = total;
      document.f_so.disc.value  = disc;
      formatNumber(document.f_so.qty,0);
      formatNumber(document.f_so.total,0);
     }	
  } else { document.f_so.qty.value = "0"; }			

}
    </script>